
const db = require("../../db")
// 用户发表评论  交流问答对应的commentNum+1
exports.addComment = (req,res)=>{
    var temp = isNaN(parseInt(req.body.parent_id))
    var parent_id
    // 处理parent_id
    if(temp){
        parent_id = null
    }else{
        parent_id = parseInt(req.body.parent_id)
    }
    // 查用户头像
    var userPhoto = null
    var str = "select photo from user where id = ?"
     db.query(str,req.user.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误，请检查数据库")
        var dataString = JSON.stringify(results)
        results = JSON.parse(dataString)
        userPhoto = results[0].photo
      
    const commentsInfo = {
        u_id:req.user.id,
        aut_name:req.user.username,
        aut_photo:userPhoto, 
        content:req.body.content,
        pubdata:new Date(),
        parent_id: parent_id,
        t_id:parseInt(req.body.t_id),
    }  
    // console.log(commentsInfo)
    // 获取发表文章的connect_Num
     str = "select * from question where id = ?"
    db.query(str,commentsInfo.t_id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误，请检查数据库")
        var dataString = JSON.stringify(results)
        results = JSON.parse(dataString)
        var commentNum = results[0].commentNum +1
        str = "insert into comments set ?"
        db.query(str,commentsInfo,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("数据库错误,请重新评论")
            // 更新文章的评论数
            str = "update question set commentNum = ? where id = ?"
            db.query(str,[commentNum,commentsInfo.t_id],(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("更新文章评论数失败")
                res.send({
                    status:0,
                    message:'发表成功',
                    data:commentsInfo
                })
            })
        })
    })
})

}

// 用户删除评论    交流问答的commentNum-1
exports.delComment = (req,res)=>{
    // 传入文章id和评论id cId,qId
    const cId = parseInt(req.body.cId)
    const qId = parseInt(req.body.qId)
    var str = "select * from question where id = ?"
    db.query(str,qId,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误,删除评论失败")
        var dataString = JSON.stringify(results)
        results = JSON.parse(dataString)
        var commentNum = results[0].commentNum
        if(commentNum <= 0){
            commentNum = 0
        }else{
            commentNum -= 1
        }
        str = "delete from comments where id = ?"
        db.query(str,cId,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("数据库错误,删除失败")
            // 更新文章评论数
            str = "update question set commentNum = ? where id = ?"
            db.query(str,[commentNum,qId],(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("更新文章评论数失败")
                res.cc("删除评论成功",0)
            })
        })
    })
}

